#include "form_drugquery.h"
#include "ui_form_drugquery.h"
#include "windows.h"
#include "commctrl.h"
#include <QFont>
#include <QDebug>
#include <QSettings>
#include <QMessageBox>
#include <QLibrary>
#include <QString>
#include <QFile>
#include <QDateTime>
#include "pub_var.h"

FormDrugQuery::FormDrugQuery(QWidget *parent)
    : QWidget(parent),
     ui(new Ui::FormDrugQuery)
{
    ui->setupUi(this);

    QSettings setIni("clin.ini", QSettings::IniFormat);
    QRect rct = setIni.value("windowState/gsize").toRect();
    this->setGeometry(rct);
    ui->statusbar->hide();
    ui->tbDetail->setVisible(false);
    ui->horizontalLayout_6->setStretch(0,1);
    ui->horizontalLayout_6->setStretch(1,0);
    ui->pushButton->setEnabled(true);
    ui->cbBalanced->setVisible(false);
    this->showNormal();

    qr = QSqlQuery(pub::db_o);
    qr0 = QSqlQuery(pub::db_o);

    QDateTime dt;

    ui->dtStart->setDate(dt.currentDateTime().date());
    ui->dtEnd->setDate(dt.currentDateTime().date());

    //ui->rbSingleDay->setChecked(true);
    ui->dtEnd->setEnabled(false);
}

FormDrugQuery::~FormDrugQuery()
{
    delete ui;
}

void FormDrugQuery::on_pushButton_clicked()
{
    ui->pushButton->setEnabled(false);
    ui->pushButton_2->setEnabled(false);
    qApp->processEvents();  //刷新界面显示
    QString sqSelect, sqWhere, sqFrom, dt1, dt2, cfSql;

    if(ui->dtStart->dateTime().date() >ui->dtEnd->dateTime().date()){
        QMessageBox::information(NULL, "注意","起始日期不能大于终止日期！");
        ui->pushButton->setEnabled(true);
        return;
    }

    dt1 = ui->dtStart->dateTime().toString("yyyy/MM/dd");
    dt2 = ui->dtEnd->dateTime().toString("yyyy/MM/dd");

    if(ui->rbSingle->isChecked()){
        if(ui->leSingle->text().isEmpty()){
            QMessageBox::information(NULL, "注意","指定单品种查询模式，但未指定具体品种名称！");
            ui->pushButton->setEnabled(true);
            return;
        }
        if(ui->cbSingleDay->isChecked()){
            sq = "select f.card_no,f.name,f.reg_date,p.apply_date,p.trade_name,p.specs,p.pack_unit,p.out_num/p.pack_qty,p.get_person"
                 " from pha_com_output p , fin_opr_register f where p.drug_dept_code = '0039' and p.drug_code = '"
                 + ui->leSingle->text() + "' and trunc(p.apply_date) = to_date('"
                 + dt1 + "','yyyy/mm/dd') and p.get_person = f.clinic_code and f.trans_type = '1' order by p.apply_date";


            int a = this->font().pointSize();
            md.setQuery(sq, pub::db_o);
            md.setHeaderData(0, Qt::Horizontal, "病志号");
            md.setHeaderData(1, Qt::Horizontal, "姓名");
            md.setHeaderData(2, Qt::Horizontal, "挂号时间");
            md.setHeaderData(3, Qt::Horizontal, "处方时间");
            md.setHeaderData(4, Qt::Horizontal, "药名");
            md.setHeaderData(5, Qt::Horizontal, "规格");
            md.setHeaderData(6, Qt::Horizontal, "单位");
            md.setHeaderData(7, Qt::Horizontal, "数量");

            ui->tbId->setModel(&md);
            ui->tbId->setColumnWidth(0, 10 * a );
            ui->tbId->setColumnWidth(1, 8 * a );
            ui->tbId->setColumnWidth(2, 12 * a );
            ui->tbId->setColumnWidth(3, 12 * a );
            ui->tbId->setColumnWidth(4, 12 * a );
            ui->tbId->setColumnWidth(5, 8 * a );
            ui->tbId->setColumnWidth(6, 4 * a );
            ui->tbId->setColumnWidth(7, 4 * a );
            ui->tbId->setColumnWidth(8, 0);
            ui->pushButton->setEnabled(true);
            ui->pushButton_2->setEnabled(true);
        }else{
            sq = "select to_char(back_date,'yyyy-mm-dd'), drug_name, specs,pack_unit, num_remain, num_consume  ";
            sq = sq + "from sun_drug_backup_daily where trunc(back_date) >=to_date('";
            sq = sq + dt1 + "','yyyy/mm/dd') and trunc(back_date) <= to_date('";
            sq = sq + dt2 + "','yyyy/mm/dd') ";
            sq = sq + " and drug_code = '"+ui->leSingle->text() +"' order by back_date";


            int a = this->font().pointSize();
            md.setQuery(sq, pub::db_o);
            md.setHeaderData(0, Qt::Horizontal, "日期");
            md.setHeaderData(1, Qt::Horizontal, "品名");
            md.setHeaderData(2, Qt::Horizontal, "规格");
            md.setHeaderData(3, Qt::Horizontal, "单位");
            md.setHeaderData(4, Qt::Horizontal, "日截止");
            md.setHeaderData(5, Qt::Horizontal, "日消耗");
            ui->tbId->setModel(&md);
            ui->tbId->setColumnWidth(0,8*a);
            ui->tbId->setColumnWidth(1,15*a);
            ui->tbId->setColumnWidth(2,8*a);
            ui->tbId->setColumnWidth(3,4*a);
            ui->tbId->setColumnWidth(4,5*a);
            ui->tbId->setColumnWidth(5,5*a);
            ui->pushButton->setEnabled(true);
            ui->pushButton_2->setEnabled(true);
        }
        return;
    }

    dt1 = ui->dtStart->dateTime().toString("yyyy/MM/dd");
    dt2 = ui->dtEnd->dateTime().toString("yyyy/MM/dd");

    sqFrom  = " FROM PHA_COM_STOCKINFO a, PHA_COM_BASEINFO b, drug_base_info c ";

    if(ui->rbKc->isChecked() ){
        //柜号,品名,规格,单位,有效期,库存数量,整件数量,零头数量, 挂账
        sqSelect = "SELECT c.position, b.TRADE_NAME, b.SPECS, b.PACK_UNIT, to_char(a.VALID_DATE,'yyyy-mm-dd'), (a.STORE_SUM / a.pack_qty) , (c.recal),"
             "CASE WHEN c.package_num = 0 THEN 0 ELSE floor((a.STORE_SUM / a.pack_qty - c.recal)/ c.package_num) END,"
             "CASE WHEN c.package_num = 0 THEN (a.STORE_SUM / a.pack_qty - c.recal) ELSE mod(a.STORE_SUM / a.pack_qty - c.recal,  c.package_num) END";
     }else if(ui->rbKcxh->isChecked()){
        sqSelect = "SELECT c.position, b.TRADE_NAME, b.SPECS, b.PACK_UNIT, to_char(a.VALID_DATE,'yyyy-mm-dd'), (a.STORE_SUM / a.pack_qty - c.recal),"
             "CASE WHEN c.package_num = 0 THEN 0 ELSE floor((a.STORE_SUM / a.pack_qty - c.recal)/ c.package_num) END,"
             "CASE WHEN c.package_num = 0 THEN (a.STORE_SUM / a.pack_qty - c.recal) ELSE mod(a.STORE_SUM / a.pack_qty - c.recal,  c.package_num) END";
        sqSelect = sqSelect + ",(select sum(out_num/pack_qty) from PHA_COM_OUTPUT d where trunc(oper_date) >=to_date('";
        sqSelect = sqSelect + dt1 + "','yyyy/mm/dd') and trunc(oper_date) <= to_date('";
        sqSelect = sqSelect + dt2 + "','yyyy/mm/dd') ";
        sqSelect = sqSelect + " and d.drug_code = a.drug_code and drug_dept_code='0039')";

    }else if(ui->rbCftj->isChecked()){
        //处方统计
        qr0.exec("delete from sun_recipe_count");    //清除旧数据
        cfSql = "select '皮肤科中药口服' lb,count(distinct o.recipe_no) ct,to_char(o.oper_date, 'yyyy-mm-dd') dt "
                " from pha_com_output o, drug_base_info b where o.drug_code = b.drug_code  and "
                " trunc(oper_date) >=to_date('" + dt1 + "','yyyy/mm/dd')  and trunc(oper_date) <= to_date('"
                + dt2 + "','yyyy/mm/dd') and department = '皮肤' and useage = '口服' and  medicine_type = 'Z' "
                "group by to_char(o.oper_date, 'yyyy-mm-dd')  "
                "  union  "
                " select '皮肤科其它' lb,count(distinct o.recipe_no) ct,to_char(o.oper_date, 'yyyy-mm-dd') dt "
                " from pha_com_output o, drug_base_info b where o.drug_code = b.drug_code and o.recipe_no in"
                " (  ( select distinct o.recipe_no from pha_com_output o, drug_base_info b"
                "  where o.drug_code = b.drug_code  and trunc(oper_date) >=to_date('" + dt1 + "','yyyy/mm/dd')  "
                " and trunc(oper_date) <= to_date('" + dt2 + "','yyyy/mm/dd') "
                " and department = '皮肤' and o.out_num > 0)"
                "    minus "
                "    (select  distinct o.recipe_no from pha_com_output o, drug_base_info b "
                "     where o.drug_code = b.drug_code and trunc(oper_date) >=to_date('" + dt1 + "','yyyy/mm/dd')"
                "  and trunc(oper_date) <= to_date('" + dt2 + "','yyyy/mm/dd')"
                "  and department = '皮肤' and useage = '口服' and  medicine_type = 'Z') )"
                "  group by to_char(o.oper_date, 'yyyy-mm-dd')   "
                " union  "
                " select '骨科' lb, count(distinct o.recipe_no) ct,to_char(o.oper_date, 'yyyy-mm-dd') dt "
                " from pha_com_output o, drug_base_info b"
                " where o.drug_code = b.drug_code  and trunc(oper_date) >=to_date('" + dt1 + "','yyyy/mm/dd') "
                " and trunc(oper_date) <= to_date('" + dt2 + "','yyyy/mm/dd') "
                " and department = '骨科' group by to_char(o.oper_date, 'yyyy-mm-dd') "
                " order by dt ";
        if(!qr.exec(cfSql)){
            qDebug()<<cfSql;
        }
qDebug()<<cfSql;
        int pz,pq,gs;
        pz = 0;
        pq = 0;
        gs = 0;
        QString dt,odt,lb;
        while(qr.next()){

            dt = qr.value(2).toString();
            lb = qr.value(0).toString();
            if(odt.isEmpty()){
                odt = dt;
            }
            if(dt != odt ){
                qr0.exec("insert into sun_recipe_count (dt, pz, pq, gs)"
                         " values ('" + odt + "',"
                         + QString::number(pz) + ","
                         + QString::number(pq) + ","
                         + QString::number(gs) + ") ");
                pz = 0;
                pq = 0;
                gs = 0;
                odt = dt;
                qDebug()<<qr0.lastQuery();
            }
            if(lb =="皮肤科中药口服" ){
                pz = qr.value(1).toInt();
            }else if(lb =="皮肤科其它" ){
                pq = qr.value(1).toInt();
            }else if(lb =="骨科" ){
                gs = qr.value(1).toInt();
            }
            qDebug()<<dt<<lb<<pz<<pq<<gs;
        }

        qr0.exec("insert into sun_recipe_count (dt, pz, pq, gs)"
                 " values ('" + dt + "',"
                 + QString::number(pz) + ","
                 + QString::number(pq) + ","
                 + QString::number(gs) + ") ");
        qDebug()<<qr0.lastQuery();

        qr0.exec("select sum(pz),sum(pq),sum(gs) from sun_recipe_count");
        qr0.next();
        pz = qr0.value(0).toInt();
        pq = qr0.value(1).toInt();
        gs = qr0.value(2).toInt();
        qr0.exec("insert into sun_recipe_count (dt, pz, pq, gs)"
                 " values ('合计',"
                 + QString::number(pz) + ","
                 + QString::number(pq) + ","
                 + QString::number(gs) + ") ");

        qDebug()<<qr0.lastQuery();
        cfSql = "select dt, pz, pq, gs, pz + pq + gs from sun_recipe_count order by dt ";
    }
    sqWhere = " where a.DRUG_CODE = c.DRUG_CODE and a.DRUG_CODE = b.DRUG_CODE "
              " and (a.STORE_SUM  / a.pack_qty ) > 0 and (a.STORE_SUM  / a.pack_qty ) > c.recal"
              " and c.valid = '1' AND DRUG_DEPT_CODE = '0039'";

    if(ui->rbCurrentDay->isChecked()) {
        sqWhere = sqWhere + " and a.drug_code in (select DISTINCT DRUG_CODE from PHA_COM_APPLYOUT ";
        //where trunc(apply_date) = trunc(sysdate)) ";
        sqWhere = sqWhere + " where trunc(apply_date) >=to_date('";
        sqWhere = sqWhere + dt1 + "','yyyy/mm/dd') and trunc(apply_date) <= to_date('";
        sqWhere = sqWhere + dt2 + "','yyyy/mm/dd')) ";
    }

    if(ui->rbSelfMade->isChecked()){ //
        sqWhere = sqWhere + " and c.come_from='制剂'";

    }
//    if(ui->rbSingle->isChecked()){ // no need more strict condition
//        sqWhere = sqWhere + " and c.drug_code='"+ui->leSingle->text()+"'";
//    }
    if(ui->rbAll->isChecked()){ // no need more strict condition
        ;
    }
    sqWhere = sqWhere + " ORDER BY c.position";

    sq = sqSelect + sqFrom + sqWhere;


    qDebug()<<sq;

    if(ui->rbCftj->isChecked()){
        md.setQuery(cfSql, pub::db_o);
    }else{
        md.setQuery(sq, pub::db_o);
    }
    int a = this->font().pointSize();

    if(ui->rbKc->isChecked()){ //storage
        //柜号,品名,规格,单位,有效期,库存数量,整件数量,零头数量
        md.setHeaderData(0, Qt::Horizontal, "柜号");
        md.setHeaderData(1, Qt::Horizontal, "品名");
        md.setHeaderData(2, Qt::Horizontal, "规格");
        md.setHeaderData(3, Qt::Horizontal, "单位");
        md.setHeaderData(4, Qt::Horizontal, "有效期");
        md.setHeaderData(5, Qt::Horizontal, "库存");
        md.setHeaderData(6, Qt::Horizontal, "挂账");
        md.setHeaderData(7, Qt::Horizontal, "件数");
        md.setHeaderData(8, Qt::Horizontal, "零数");


        ui->tbId->setModel(&md);
        ui->tbId->setColumnWidth(0,4*a);
        ui->tbId->setColumnWidth(1,15*a);
        ui->tbId->setColumnWidth(2,8*a);
        ui->tbId->setColumnWidth(3,4*a);
        ui->tbId->setColumnWidth(4,8*a);
        ui->tbId->setColumnWidth(5,4*a);
        ui->tbId->setColumnWidth(6,4*a);
        ui->tbId->setColumnWidth(7,4*a);

    }else if(ui->rbKcxh->isChecked()){
        md.setHeaderData(0, Qt::Horizontal, "柜号");
        md.setHeaderData(1, Qt::Horizontal, "品名");
        md.setHeaderData(2, Qt::Horizontal, "规格");
        md.setHeaderData(3, Qt::Horizontal, "单位");
        md.setHeaderData(4, Qt::Horizontal, "有效期");
        md.setHeaderData(5, Qt::Horizontal, "库存");
        md.setHeaderData(6, Qt::Horizontal, "件数");
        md.setHeaderData(7, Qt::Horizontal, "零数");
        md.setHeaderData(8, Qt::Horizontal, "消耗");

        ui->tbId->setModel(&md);
        ui->tbId->setColumnWidth(0,4*a);
        ui->tbId->setColumnWidth(1,15*a);
        ui->tbId->setColumnWidth(2,8*a);
        ui->tbId->setColumnWidth(3,4*a);
        ui->tbId->setColumnWidth(4,8*a);
        ui->tbId->setColumnWidth(5,4*a);
        ui->tbId->setColumnWidth(6,4*a);
        ui->tbId->setColumnWidth(7,4*a);
        ui->tbId->setColumnWidth(8,4*a);

    }else if(ui->rbCftj->isChecked()){
        md.setHeaderData(0, Qt::Horizontal, "日期");
        md.setHeaderData(1, Qt::Horizontal, "皮肤中药");
        md.setHeaderData(2, Qt::Horizontal, "皮肤其它");
        md.setHeaderData(3, Qt::Horizontal, "骨科");
        md.setHeaderData(4, Qt::Horizontal, "合计");


        ui->tbId->setModel(&md);
        ui->tbId->setColumnWidth(0, 10*a);
        ui->tbId->setColumnWidth(1, 10*a);
        ui->tbId->setColumnWidth(2, 10*a);
        ui->tbId->setColumnWidth(3, 10*a);
        ui->tbId->setColumnWidth(3, 10*a);

    }
    ui->pushButton->setEnabled(true);
    ui->pushButton_2->setEnabled(true);

}

void FormDrugQuery::on_pushButton_2_clicked()
{
    close();

}

void FormDrugQuery::on_rbSingle_clicked()
{
    ui->groupBox_3->setEnabled(false);
    ui->gbDate->setEnabled(true);
    ui->cbSingleDay->setChecked(true);
    ui->dtEnd->setEnabled(true);
    ui->pushButton->setEnabled(true);
    ui->tbDetail->setVisible(false);
    ui->horizontalLayout_6->setStretch(0,1);
    ui->horizontalLayout_6->setStretch(1,0);
    ui->cbBalanced->setVisible(false);

    sq = "SELECT position, drug_name,specs, drug_code from drug_base_info order by position ";

    md.setQuery(sq, pub::db_o);

    int a = this->font().pointSize();

    md.setHeaderData(0, Qt::Horizontal, "柜号");
    md.setHeaderData(1, Qt::Horizontal, "品名");
    md.setHeaderData(2, Qt::Horizontal, "规格");
    md.setHeaderData(3, Qt::Horizontal, "药品编码");
    ui->tbId->setModel(&md);
    ui->tbId->setColumnWidth(0,4*a);
    ui->tbId->setColumnWidth(1,15*a);
    ui->tbId->setColumnWidth(2,8*a);
    ui->tbId->setColumnWidth(3,10*a);

}

void FormDrugQuery::on_tbId_doubleClicked(const QModelIndex &index)
{
    if(!ui->rbSingle->isChecked()){
        return;
    }
    QString str;
    str = index.siblingAtColumn(3).data(0).toString();
    ui->leSingle->setText(str);

}

void FormDrugQuery::on_dtStart_dateChanged(const QDate &date)
{
    if(ui->cbSingleDay->isChecked()){
        ui->dtEnd->setDate(date);
    }
}

void FormDrugQuery::on_cbSingleDay_clicked()
{
    if(ui->cbSingleDay->isChecked()){
        ui->dtEnd->setEnabled(false);
    }else{
        ui->dtEnd->setEnabled(true);
    }

}

void FormDrugQuery::on_rbCurrentDay_clicked()
{
    setState();
}

void FormDrugQuery::on_rbSelfMade_clicked()
{
    setState();
}

void FormDrugQuery::on_rbAll_clicked()
{
    setState();
}

//void FormDrugQuery::on_ckConsume_clicked()
//{
//    setState();
//}

void FormDrugQuery::setState()
{
    ui->gbDate->setEnabled(false);//预置状态
    ui->rbKc->setEnabled(false);
    ui->rbKcxh->setEnabled(false);
    ui->groupBox->setEnabled(true);
    ui->groupBox_3->setEnabled(true);
    ui->tbDetail->setVisible(false);
    ui->horizontalLayout_6->setStretch(0,1);
    ui->horizontalLayout_6->setStretch(1,0);
    ui->pushButton->setEnabled(true);
    ui->cbBalanced->setVisible(false);
    ui->leSingle->clear();


    if(ui->rbCurrentDay->isChecked()){
        ui->gbDate->setEnabled(true);
        ui->cbSingleDay->setChecked(true);
        ui->dtEnd->setEnabled(false);
        ui->rbKc->setEnabled(true);
        ui->rbKcxh->setEnabled(true);
    }else if(ui->rbSelfMade->isChecked() || ui->rbAll->isChecked()){
        ui->rbKc->setEnabled(true);
        ui->rbKcxh->setEnabled(true);

    }else if(ui->rbSingle->isChecked()){
        ui->rbKc->setEnabled(false);
        ui->rbKcxh->setEnabled(false);
        return;
    }

    if(ui->rbKcxh->isChecked() || ui->rbCftj->isChecked()){
        ui->gbDate->setEnabled(true);
    }
    if(ui->rbCftj->isChecked()){
        ui->groupBox->setEnabled(false);
        ui->gbDate->setEnabled(true);
        ui->dtStart->setEnabled(true);
        ui->dtEnd->setEnabled(true);
    }
}

void FormDrugQuery::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/gsize", QVariant(this->geometry()));
    emit destroyW( "FormDrugQuery" );
}

void FormDrugQuery::on_rbKc_clicked()
{
    setState();
}
void FormDrugQuery::on_rbKcxh_clicked()
{
    setState();
}
void FormDrugQuery::on_rbCftj_clicked()
{
    setState();
}

void FormDrugQuery::on_rbHang_clicked()
{
    ui->groupBox_3->setEnabled(false);
    ui->gbDate->setEnabled(true);
    ui->cbSingleDay->setChecked(false);
    ui->dtEnd->setEnabled(true);

    ui->tbDetail->setVisible(true);
    ui->horizontalLayout_6->setStretch(0,2);
    ui->horizontalLayout_6->setStretch(1,3);
    ui->pushButton->setEnabled(false);
    ui->cbBalanced->setVisible(true);

    if(ui->cbBalanced->isChecked()){
        sq = "SELECT position, drug_name,specs,recal,drug_code from drug_base_info where recal > 0 order by position ";
    }else{
        sq = "SELECT position, drug_name,specs,recal,drug_code from drug_base_info where valid = '1' order by position ";
    }

    md.setQuery(sq, pub::db_o);

    //int a = this->font().pointSize();
    int a = ui->tbId->width() - 50;

    md.setHeaderData(0, Qt::Horizontal, "柜号");
    md.setHeaderData(1, Qt::Horizontal, "品名");
    md.setHeaderData(2, Qt::Horizontal, "规格");
    md.setHeaderData(3, Qt::Horizontal, "数量");
    ui->tbId->setModel(&md);
    //15,45,25,15

    ui->tbId->setColumnWidth(0, a * .15);
    ui->tbId->setColumnWidth(1, a * .45);
    ui->tbId->setColumnWidth(2, a * .25);
    ui->tbId->setColumnWidth(3, a * .15);
    ui->tbId->setColumnWidth(4, 0);

}

void FormDrugQuery::on_tbId_clicked(const QModelIndex &index)
{
    if(!ui->rbHang->isChecked()){
        return;
    }
    QString drug_code;
    drug_code = index.siblingAtColumn(4).data(0).toString();

    QString tmpCondition = "";
    if(ui->cbBalanced->isChecked()){
        tmpCondition = " and oper_date > (select nvl(max(oper_date), to_date('2000/1/1','yyyy/mm/dd')) "
                       " from drug_sub_account where balanced = '1' and drug_code = '" + drug_code + "')";
    }

    sq = "select to_char(oper_date, 'yyyy-mm-dd') 日期, oper_sum 数量, oper_name 操作员,  "
         " (case when in_way is null then out_way else in_way end) 变更方式, "
         " (case when leader is not null then concat(concat( leader,'，'), memo) else memo end) 备注 , item_io_id from drug_sub_account "
         "where oper_sum <> 0 and drug_code = '" + drug_code + "' " + tmpCondition + "order by oper_date ";

    mdRecal.setQuery(sq, pub::db_o);

    int a = ui->tbDetail->width() - 50;
    //QMessageBox::information(this,"note",QString::number(a));
    //mdRecal.setHeaderData(0, Qt::Horizontal, "柜号");
    //mdRecal.setHeaderData(1, Qt::Horizontal, "品名");
    //mdRecal.setHeaderData(2, Qt::Horizontal, "规格");
    //mdRecal.setHeaderData(3, Qt::Horizontal, "挂账数量");
    ui->tbDetail->setModel(&mdRecal);
    //30+15+20+30+105

    ui->tbDetail->setColumnWidth(0, a * .2);
    ui->tbDetail->setColumnWidth(1, a * .1);
    ui->tbDetail->setColumnWidth(2, a * .1);
    ui->tbDetail->setColumnWidth(3, a * .15);
    ui->tbDetail->setColumnWidth(4, a * .45);
    ui->tbDetail->setColumnWidth(5, 0 );

}

void FormDrugQuery::on_cbBalanced_clicked()
{

    on_rbHang_clicked();
    mdRecal.setQuery("", pub::db_o);
    //on_tbId_clicked( ui->tbId->currentIndex());
}

void FormDrugQuery::on_tbDetail_doubleClicked(const QModelIndex &index)
{
    //if(!ui->rbSingle->isChecked()){
    //    return;
    //}
    sq = "select drug_name,oper_sum, leader, memo from drug_sub_account where item_io_id = '"
            + index.siblingAtColumn(5).data(0).toString() + "'";

    if(!qr.exec(sq)){
        QMessageBox::warning(this,"注意","查询sql执行失败！");
        return;
    }
    qr.next();
    QString str = qr.value(3).toString();
    str.append("\n");

    while(true){
        str.append(qr.value(0).toString());
        str.append("\t");
        str.append(qr.value(1).toString());
        if(!qr.next()){
            break;
        }
        str.append("\n");
    }
    QMessageBox::warning(this,"明细", str);
}
